<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/6/6 0006
 * Time: 16:30
 */
namespace app\api\controller;
vendor('alipay.AopClient');
class AliPay{
    /**
     * 配置参数
     * @var array
     */
    var $config = array(
        'appId'      => '2017041406712362',
        'privateKey' => 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDSPKt0dSj8tx8JbHyVVopYfi8w0DlWo6AiHT2WsewPEZxBLqkoe2CJUSDVheo+6cE4g+nR2kRTooDyGN4XEUzGrOB2FAuXOf6Md+VKYSiifK6L/Q/CB9nE6ds9MOIcF+PwY9E0O6M6d5bjpF5fFGWRk8PsklUWxiAZSvQ3Lil2flFziK2cg+5R+T8MY3o33FsaPUKKtAvZ+RBg4xxj96ddjv5oGixYIuXi2f0Lpur64lba2dal/oibqqZ2+Raw4VHIXfw1gskP4YnOOpN/WMlpulzq9SFecYNVJKur5/wtp2CI5GxeZ0lvZrXD0NbI4wDbPm9Wq/3TCScW7U6rMaDdAgMBAAECggEBAJLGXIUvjm/wgLCZltjD1E43ukZ/+KfuEEwA0hC1IrLafpZbNynWCO2VQDESDxnhJE6KeeFSWsVw/EkI53R/VXSXGRYWpIjtqjLzOZcWAcJ+/XUqNagmxvhcs/0idZlbbH1Vkqa+xBR7jVJcXK5QlSBvYyd0egcuRxahZ6Jmjff0rv4CbENBpxKQ2xMdgJFqluUaZkRAPY/65zoGjsDx5KOBBpNuxJIR4FXg6NK1gETna2eGrDupQ2L40uZPJ0LtvwVH6kH52bEfNGO6E24b/TRm0cIySjsQkZiwMP0ezenk2qAg1Th6/W/xOU7Db2zuSmqI8AyZReLfNQSm3GOblgECgYEA/65583+Et8pjBhqUW0oEi5ao1rZVJT8EKfBAoWULdk28bkSVHtm13x48ZzwZvFvCzmnzlP9ZgrmseeFQ0io6iKYn+9LVGv1OvodMTczGQXZkq53BXB65tia7veFzkLUohUnpxgVxSHsUJ0RA3R+Z72jmp+8bcUnYq+wyXMrQaEECgYEA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MA2z5vVqv90wknFu1OqzGg3QIDAQAB',
        'notifyUrl'  => 'http://project.itspire.cn/api/callback/alipayCallback',
    );

    /**
     * 支付宝预支付
     * @param $subject
     * @param $out_trade_no
     * @param $total_amount
     * @return string
     */
    function aliPay($subject,$out_trade_no,$total_amount){
        // 开始支付
        $aop = new \AopClient;
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $this->config['appId'];
        $aop->rsaPrivateKey = $this->config['privateKey'];
        $aop->alipayrsaPublicKey = $this->config['publicKey'];
        $aop->format   = "json";
        $aop->charset  = "UTF-8";
        $aop->signType = "RSA2";
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request = new \AlipayTradeAppPayRequest;
        //SDK已经封装掉了公共参数，这里只需要传入业务参数
        $bizcontent = "{
               \"subject\": \"$subject\","
            . "\"out_trade_no\": \"$out_trade_no\","
            . "\"timeout_express\": \"30m\","
            . "\"total_amount\": \"$total_amount\","
            . "\"product_code\":\"QUICK_MSECURITY_PAY\""
            . "}";
        $request->setNotifyUrl($this->config['notifyUrl']); // 异步回调地址
        $request->setBizContent($bizcontent);
        //这里和普通的接口调用不同，使用的是sdkExecute
        $response = @$aop->sdkExecute($request);
        return $response;
    }

    /**
     * 支付宝退款
     * @param $order_id
     * @param $trade_id
     * @param $order_amount
     * @throws \Exception
     */
    function aliRefund($order_id,$trade_id,$order_amount){
        $aop = new \AopClient;
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $this->config['appId'];
        $aop->rsaPrivateKey = $this->config['privateKey'];
        $aop->alipayrsaPublicKey = $this->config['publicKey'];
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new \AlipayTradeRefundRequest;
        $request->setBizContent("{" .
            "\"out_trade_no\":\"$order_id\"," .
            "\"trade_no\":\"$trade_id\"," .
            "\"refund_amount\":$order_amount," .
            "\"refund_reason\":\"正常退款\"," .
            "\"out_request_no\":\"HZ01RF001\"," .
            "\"operator_id\":\"OP001\"," .
            "\"store_id\":\"NJ_S_001\"," .
            "\"terminal_id\":\"NJ_T_001\"" .
            "  }");
        $result = $aop->execute($request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        return $resultCode;
    }
}